package cn.mlong.demo.convert

object HexConverter {

    fun f16to10(hex: String): String {
        return try {
            if (hex.contains("0x") or hex.contains("0X")) {
                val pureHex = hex.removePrefix("0x").removePrefix("0X")
                pureHex.toLong(16).toString() // 明确指定这是16进制数
            } else {
                hex
            }
        } catch (e: NumberFormatException) {
            e.printStackTrace()
            "无效的十六进制数字: $hex"
        }
    }

    fun f10to16(hex: String): String {
        return try {
            if (hex.contains("0x") or hex.contains("0X")) {
                hex
            } else {
                "0x" + (Integer.toHexString(hex.toInt()).uppercase())
            }
        } catch (e: NumberFormatException) {
            e.printStackTrace()
            "无效的十六进制数字"
        }
    }
}

